package com.pignest.hotel.servlet.report;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.report.MemberCharge;
import com.pignest.hotel.service.ReportService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.util.DateUtil;

public class MemberChargeReport extends BaseReport {
	
	@Override
	public void initParameters(HttpServletRequest req) {
		super.initParameters(req);
		notNull(startTime);
		if (endTime == null)
			endTime = new Date();
		assertTrue(endTime.getTime() > startTime.getTime());
		assertTrue(endTime.getTime() - startTime.getTime() < DateUtil.ONE_DAY * 31);
	}
	
	@Override
	public String getTimeFormat() {
		return DATE_FORMAT;
	}

	@Override
	public String getReportName() {
		return "会员充值";
	}

	@Override
	public JRDataSource getDataSource(HttpServletRequest req) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(startTime);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		long end = endTime.getTime();
		ReportService service = Services.getReportService();
		List<MemberCharge> cs = new ArrayList<MemberCharge>();
		while (cal.getTimeInMillis() <= end) {
			Date s = cal.getTime();
			cal.add(Calendar.DAY_OF_MONTH, 1);
			Date e = cal.getTime();
			long v = service.reportMemberCharge(s, e);
			String time = DateUtil.toString(s, DATE_FORMAT);
			String price = Utils.toPrice(v);
			cs.add(new MemberCharge(time, price));
		}
		return new JRBeanCollectionDataSource(cs);
	}
}
